package cn.lopr.im.android.utils.cos;

import android.content.Context;
import android.util.Log;

import com.tencent.cos.xml.CosXmlService;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.exception.CosXmlClientException;
import com.tencent.cos.xml.exception.CosXmlServiceException;
import com.tencent.cos.xml.listener.CosXmlResultListener;
import com.tencent.cos.xml.model.CosXmlRequest;
import com.tencent.cos.xml.model.CosXmlResult;
import com.tencent.cos.xml.transfer.COSXMLUploadTask;
import com.tencent.cos.xml.transfer.TransferConfig;
import com.tencent.cos.xml.transfer.TransferManager;

import cn.lopr.im.android.common.OnBack;

public class CosClient {

    private final CosXmlService cosXmlService;
    private final Context context;
    private final CosProperties cosProperties;
    private MySessionCredentialProvider myCredentialProvider;
    private String TAG;

    public CosClient(Context context, MySessionCredentialProvider myCredentialProvider, CosProperties cosProperties) {
        this.context = context;
        this.cosProperties = cosProperties;
        this.myCredentialProvider = myCredentialProvider;

        CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
                .setRegion(this.cosProperties.getRegion())
                .isHttps(true)
                .builder();
        this.cosXmlService = new CosXmlService(context, serviceConfig, myCredentialProvider);
    }

    /**
     * @param cosPath  对象在存储桶中的位置标识符，即称对象键
     * @param srcPath  本地文件的绝对路径
     * @param uploadId 若存在初始化分块上传的 UploadId，则赋值对应的 uploadId 值用于续传；否则，赋值 null
     */
    public void upload(String cosPath, String srcPath, String uploadId, OnBack<String> onBack) {

        TransferConfig transferConfig = new TransferConfig.Builder().build();
        TransferManager transferManager = new TransferManager(cosXmlService, transferConfig);

        COSXMLUploadTask cosxmlUploadTask = transferManager.upload(this.cosProperties.getBucket()
                , cosPath, srcPath, uploadId);

        //设置上传进度回调
        cosxmlUploadTask.setCosXmlProgressListener((complete, target) -> {
            // todo Do something to update progress...
        });
        //设置返回结果回调
        cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
            @Override
            public void onSuccess(CosXmlRequest request, CosXmlResult result) {
                COSXMLUploadTask.COSXMLUploadTaskResult cOSXMLUploadTaskResult =
                        (COSXMLUploadTask.COSXMLUploadTaskResult) result;
                String accessUrl = cOSXMLUploadTaskResult.accessUrl;
                onBack.success(accessUrl);
                Log.i(TAG, "onSuccess: " + accessUrl);
            }

            @Override
            public void onFail(CosXmlRequest request, CosXmlClientException clientException,
                               CosXmlServiceException serviceException) {
                if (clientException != null) {
                    clientException.printStackTrace();
                } else {
                    serviceException.printStackTrace();
                }
            }
        });
        //设置任务状态回调, 可以查看任务过程
        cosxmlUploadTask.setTransferStateListener(state -> {
            // todo notify transfer state
        });
    }
}
